import React from 'react';
import { Button } from 'antd';
import { inject, observer } from 'mobx-react';
import { WeaTools, WeaErrorPage, WeaAuth } from 'ecCom';
import { toJS } from 'mobx';
import { WeaTableNew } from 'comsMobx';
import './style/journal.less';

const WeaTable = WeaTableNew.WeaTable;
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@inject("journalStore")
@observer
class JournalList extends React.Component {

    constructor(props) {
        super(props);
    }

    doInit(props) {
        const { journalStore, params:{kwlx} } = props;
        const { initList } = journalStore;
        initList({kwlx:kwlx});
    }

    componentDidMount() {
        this.doInit(this.props);
    }

    componentWillReceiveProps(nextProps) {
        if (this.props.location.key !== nextProps.location.key || this.props.params.kwlx !== nextProps.params.kwlx) {
            this.doInit(nextProps);
        }
    }

    // shouldComponentUpdate(nextProps, nextState) {
    //     return true;
    // }

    // componentWillUnmount() {
    //     const { myInventoryStore } = this.props;
    //     const {form } = myInventoryStore;
    //     form.resetConditionValue(); 
    //     myInventoryStore.clearStatus();
    // }

    render() {
        const { journalStore } = this.props;
        const { tableStore, tableStore1, addJournal, dingkan, batchDelete, resume, reLoad, isShowShare, showAddShareModal, publish, cancelPublish } = journalStore;
        const tableHeight = ((document.body.clientHeight-54-20)/2)-41-40-44;
        const disabled = !tableStore.selectedRowKeysAllPages.length>0;
        const disabled1 = !tableStore1.selectedRowKeysAllPages.length>0;
        let shareCondition = [
            {
                "colSpan": 2,
                "conditionType": "SELECT",
                "domkey": [
                    "sharetype"
                ],
                "fieldcol": 16,
                "isQuickSearch": false,
                "label": getLabel( 21956 ,"对象类型"),
                "labelcol": 7,
                "options": [
                    {
                        "key": "1",
                        "selected": true,
                        "showname": getLabel(179,"人力资源")
                    }, {
                        "key": "2",
                        "selected": false,
                        "showname": getLabel(141,"分部")
                    }, {
                        "key": "3",
                        "selected": false,
                        "showname": getLabel(124,"部门")
                    }, {
                        "key": "6",
                        "selected": false,
                        "showname": getLabel(6086,"岗位")
                    }, {
                        "key": "4",
                        "selected": false,
                        "showname": getLabel(122,"角色")
                    }, {
                        "key": "5",
                        "selected": false,
                        "showname": getLabel(1340,"所有人")
                    }
                ],
                "viewAttr": 2
            }, {
                "3": [[
                        {
                            "browserConditionParam": {
                                "completeParams": {},
                                "conditionDataParams": {},
                                "dataParams": {},
                                "destDataParams": {},
                                "hasAddBtn": false,
                                "hasAdvanceSerach": true,
                                "idSeparator": ",",
                                "isAutoComplete": 1,
                                "isDetail": 0,
                                "isMultCheckbox": false,
                                "isSingle": false,
                                "pageSize": 10,
                                "quickSearchName": "",
                                "type": "57",
                                "viewAttr": 3
                            },
                            "colSpan": 2,
                            "conditionType": "BROWSER",
                            "domkey": [
                                "relatedshareid"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(106,"对象"),
                            "labelcol": 7,
                            "viewAttr": 3
                        }
                    ],[
                        {
                            "colSpan": 2,
                            "conditionType": "INPUT_INTERVAL",
                            "needDefaultValue":false,
                            "domkey": [
                                "seclevel",
                                "seclevelMax"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(683,"安全级别"),
                            "labelcol": 7,
                            "value": [
                                0,
                                ''
                            ],
                            "viewAttr": 3
                        }
                    ],[
                        {
                            "colSpan": 2,
                            "conditionType": "SELECT",
                            "domkey": [
                                "sharelevel"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(385,"权限"),
                            "labelcol": 7,
                            "options": [
                                {
                                    "key": "1",
                                    "selected": true,
                                    "showname": getLabel( 367 ,"查看")
                                // }, {
                                //     "key": "2",
                                //     "selected": false,
                                //     "showname": getLabel( 93 ,"编辑")
                                }
                            ],
                            "viewAttr": 2
                        }
                    ]
                ],
                "2": [[
                        {
                            "browserConditionParam": {
                                "completeParams": {},
                                "conditionDataParams": {},
                                "dataParams": {},
                                "destDataParams": {},
                                "hasAddBtn": false,
                                "hasAdvanceSerach": true,
                                "idSeparator": ",",
                                "isAutoComplete": 1,
                                "isDetail": 0,
                                "isMultCheckbox": false,
                                "isSingle": false,
                                "pageSize": 10,
                                "quickSearchName": "",
                                "type": "194",
                                "viewAttr": 3
                            },
                            "colSpan": 2,
                            "conditionType": "BROWSER",
                            "domkey": [
                                "relatedshareid"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(106,"对象"),
                            "labelcol": 7,
                            "value": 3,
                            "viewAttr": 2
                        }
                    ],[
                        {
                            "colSpan": 2,
                            "conditionType": "INPUT_INTERVAL",
                            "needDefaultValue":false,
                            "domkey": [
                                "seclevel",
                                "seclevelMax"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(683,"安全级别"),
                            "labelcol": 7,
                            "value": [
                                0,
                                ''
                            ],
                            "viewAttr": 3
                        }
                    ],[
                        {
                            "colSpan": 2,
                            "conditionType": "SELECT",
                            "domkey": [
                                "sharelevel"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(385,"权限"),
                            "labelcol": 7,
                            "options": [
                                {
                                    "key": "1",
                                    "selected": true,
                                    "showname": getLabel( 367 ,"查看")
                                // }, {
                                //     "key": "2",
                                //     "selected": false,
                                //     "showname": getLabel( 93 ,"编辑")
                                }
                            ],
                            "viewAttr": 2
                        }
                    ]
                ],
                "1": [[
                        {
                            "browserConditionParam": {
                                "completeParams": {},
                                "conditionDataParams": {},
                                "dataParams": {},
                                "destDataParams": {},
                                "hasAddBtn": false,
                                "hasAdvanceSerach": true,
                                "idSeparator": ",",
                                "isAutoComplete": 1,
                                "isDetail": 0,
                                "isMultCheckbox": false,
                                "isSingle": false,
                                "pageSize": 10,
                                "quickSearchName": "",
                                "type": "17",
                                "viewAttr": 3
                            },
                            "colSpan": 2,
                            "conditionType": "BROWSER",
                            "domkey": [
                                "relatedshareid"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(106,"对象"),
                            "labelcol": 7,
                            "viewAttr": 3
                        }
                    ],[
                        {
                            "colSpan": 2,
                            "conditionType": "SELECT",
                            "domkey": [
                                "sharelevel"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(385,"权限"),
                            "labelcol": 7,
                            "options": [
                                {
                                    "key": "1",
                                    "selected": true,
                                    "showname": getLabel( 367 ,"查看")
                                // }, {
                                //     "key": "2",
                                //     "selected": false,
                                //     "showname": getLabel( 93 ,"编辑")
                                }
                            ],
                            "viewAttr": 2
                        }
                    ]
                ],
                "6": [[
                        {
                            "browserConditionParam": {
                                "completeParams": {},
                                "conditionDataParams": {},
                                "dataParams": {},
                                "destDataParams": {},
                                "hasAddBtn": false,
                                "hasAdvanceSerach": true,
                                "idSeparator": ",",
                                "isAutoComplete": 1,
                                "isDetail": 0,
                                "isMultCheckbox": false,
                                "isSingle": false,
                                "pageSize": 10,
                                "quickSearchName": "",
                                "type": "278",
                                "viewAttr": 3
                            },
                            "colSpan": 2,
                            "conditionType": "BROWSER",
                            "domkey": [
                                "relatedshareid"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(106,"对象"),
                            "labelcol": 7,
                            "viewAttr": 3
                        }
                    ],[
                        {
                            "colSpan": 2,
                            "conditionType": "SELECT_LINKAGE",
                            "domkey": [
                                "joblevel"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel( 28169 ,"岗位级别"),
                            "labelcol": 7,
                            "options": [
                                {
                                    "key": "0",
                                    "selected": true,
                                    "showname": getLabel( 140 ,"总部")
                                }, {
                                    "key": "2",
                                    "selected": false,
                                    "showname": getLabel( 19437 ,"指定分部")
                                }, {
                                    "key": "1",
                                    "selected": false,
                                    "showname": getLabel( 19438 ,"指定部门")
                                }
                            ],
                            "selectLinkageDatas": {
                                "1": {
                                    "browserConditionParam": {
                                        "completeParams": {},
                                        "conditionDataParams": {},
                                        "dataParams": {},
                                        "destDataParams": {},
                                        "hasAddBtn": false,
                                        "hasAdvanceSerach": true,
                                        "idSeparator": ",",
                                        "isAutoComplete": 1,
                                        "isDetail": 0,
                                        "isMultCheckbox": false,
                                        "isSingle": false,
                                        "pageSize": 10,
                                        "quickSearchName": "",
                                        "type": "57",
                                        "viewAttr": 3
                                    },
                                    "colSpan": 2,
                                    "conditionType": "BROWSER",
                                    "domkey": [
                                        "jobtitledepartment"
                                    ],
                                    "fieldcol": 16,
                                    "isQuickSearch": false,
                                    "label": "",
                                    "labelcol": 7,
                                    "viewAttr": 3
                                },
                                "2": {
                                    "browserConditionParam": {
                                        "completeParams": {},
                                        "conditionDataParams": {},
                                        "dataParams": {},
                                        "destDataParams": {},
                                        "hasAddBtn": false,
                                        "hasAdvanceSerach": true,
                                        "idSeparator": ",",
                                        "isAutoComplete": 1,
                                        "isDetail": 0,
                                        "isMultCheckbox": false,
                                        "isSingle": false,
                                        "pageSize": 10,
                                        "quickSearchName": "",
                                        "type": "194",
                                        "viewAttr": 3
                                    },
                                    "colSpan": 2,
                                    "conditionType": "BROWSER",
                                    "domkey": [
                                        "jobtitlesubcompany"
                                    ],
                                    "fieldcol": 16,
                                    "isQuickSearch": false,
                                    "label": "",
                                    "labelcol": 7,
                                    "viewAttr": 3
                                }
                            },
                            "viewAttr": 2
                        }
                    ],[
                        {
                            "colSpan": 2,
                            "conditionType": "SELECT",
                            "domkey": [
                                "sharelevel"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(385,"权限"),
                            "labelcol": 7,
                            "options": [
                                {
                                    "key": "1",
                                    "selected": true,
                                    "showname": getLabel( 367 ,"查看")
                                // }, {
                                //     "key": "2",
                                //     "selected": false,
                                //     "showname": getLabel( 93 ,"编辑")
                                }
                            ],
                            "viewAttr": 2
                        }
                    ]
                ],
                "5": [[
                        {
                            "colSpan": 2,
                            "conditionType": "INPUT_INTERVAL",
                            "needDefaultValue":false,
                            "domkey": [
                                "seclevel",
                                "seclevelMax"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(683,"安全级别"),
                            "labelcol": 7,
                            "value": [
                                0,
                                ''
                            ],
                            "viewAttr": 3
                        }
                    ],[
                        {
                            "colSpan": 2,
                            "conditionType": "SELECT",
                            "domkey": [
                                "sharelevel"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(385,"权限"),
                            "labelcol": 7,
                            "options": [
                                {
                                    "key": "1",
                                    "selected": true,
                                    "showname": getLabel( 367 ,"查看")
                                // }, {
                                //     "key": "2",
                                //     "selected": false,
                                //     "showname": getLabel( 93 ,"编辑")
                                }
                            ],
                            "viewAttr": 2
                        }
                    ]
                ],
                "4": [[
                        {
                            "browserConditionParam": {
                                "completeParams": {},
                                "conditionDataParams": {},
                                "dataParams": {},
                                "destDataParams": {},
                                "hasAddBtn": false,
                                "hasAdvanceSerach": true,
                                "idSeparator": ",",
                                "isAutoComplete": 1,
                                "isDetail": 0,
                                "isMultCheckbox": false,
                                "isSingle": false,
                                "pageSize": 10,
                                "quickSearchName": "",
                                "type": "65",
                                "viewAttr": 3
                            },
                            "colSpan": 2,
                            "conditionType": "BROWSER",
                            "domkey": [
                                "relatedshareid"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(106,"对象"),
                            "labelcol": 7,
                            "viewAttr": 3
                        }, {
                            "colSpan": 2,
                            "conditionType": "SELECT",
                            "domkey": [
                                "rolelevel"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": "级别",
                            "labelcol": 7,
                            "options": [
                                {
                                    "key": "0",
                                    "selected": true,
                                    "showname": getLabel(124,"部门")
                                }, {
                                    "key": "1",
                                    "selected": false,
                                    "showname":  getLabel(141,"分部")
                                }, {
                                    "key": "2",
                                    "selected": false,
                                    "showname":  getLabel(140,"总部")
                                }
                            ],
                            "viewAttr": 2
                        }
                    ],[
                        {
                            "colSpan": 2,
                            "conditionType": "INPUT_INTERVAL",
                            "needDefaultValue":false,
                            "domkey": [
                                "seclevel",
                                "seclevelMax"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(683,"安全级别"),
                            "labelcol": 7,
                            "value": [
                                0,
                                ''
                            ],
                            "viewAttr": 3
                        }
                    ],[
                        {
                            "colSpan": 2,
                            "conditionType": "SELECT",
                            "domkey": [
                                "sharelevel"
                            ],
                            "fieldcol": 16,
                            "isQuickSearch": false,
                            "label": getLabel(385,"权限"),
                            "labelcol": 7,
                            "options": [
                                {
                                    "key": "1",
                                    "selected": true,
                                    "showname": getLabel( 367 ,"查看")
                                // }, {
                                //     "key": "2",
                                //     "selected": false,
                                //     "showname": getLabel( 93 ,"编辑")
                                }
                            ],
                            "viewAttr": 2
                        }
                    ]
                ]
            }
        ]
        return (
            <div className='journalList'>
                <div className='list1'>
                    <div className='button'>
                        <div className='add'>
                            <Button type="primary" onClick={() =>{addJournal()}}>新增</Button>
                        </div>
                        <div className='approve'>
                            <Button type="primary" disabled={disabled} onClick={() =>{dingkan()}}>定刊</Button>
                        </div>
                        <div className='delete'>
                            <Button type="primary" disabled={disabled} onClick={() =>{batchDelete()}}>删除</Button>
                        </div>
                    </div>
                    <div className='table'>
                        <WeaTable 
                            comsWeaTableStore={tableStore}
                            hasOrder={true}
                            needScroll={true}
                            scroll={{y:tableHeight}}
                            tableWidth={this.tableWidth}
                            register_table={reLoad}
                        />
                    </div>
                </div>
                <div className='list2'>
                    <div className='button'>
                        <div className='resume'>
                            <Button type="primary" disabled={disabled1} onClick={() =>{resume()}}>恢复</Button>
                        </div>
                        <div className='resume'>
                            <Button type="primary" disabled={disabled1} onClick={() =>{showAddShareModal(true)}}>发布到门户</Button>
                        </div>
                        <div className='resume'>
                            <Button type="primary" disabled={disabled1} onClick={() =>{cancelPublish()}}>撤销发布</Button>
                        </div>
                        <div className='resume'>
                            <Button type="primary" disabled={disabled1} onClick={() =>{}}>发布到移动端</Button>
                        </div>
                        <div className='resume'>
                            <Button type="primary" disabled={disabled1} onClick={() =>{}}>取消发布</Button>
                        </div>
                    </div>
                    <div className='table'>
                        <WeaTable 
                            comsWeaTableStore={tableStore1}
                            hasOrder={true}
                            needScroll={true}
                            scroll={{y:tableHeight}}
                            tableWidth={this.tableWidth}
                            register_table={reLoad}
                            // onOperatesClick={this.onOperatesClick}
                        />
                        <WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@o5lbdm`} 
                            visible={isShowShare}
                            conditions={toJS(shareCondition)}
                            title={getLabel(83476,"添加")+getLabel(2112,"共享设置")}
                            icon={"icon-coms-fa"}
                            iconBgcolor="#f14a2d"
                            onOk={v => {
                                publish(v);
                            }}
                            onCancel={() => {
                                showAddShareModal(false);
                            }}
                        />
                    </div>
                </div>
            </div>
        )
    }

    tableWidth = (columns) => {
        let width = 0;
        let cols = 0;
        columns.map(col => {
          if (col.display === 'true') {
            let oldwidth = col.oldWidth || '10%';
            let widthInt = parseFloat(oldwidth.replace('%', ''));
            if (widthInt <= 0) {
              widthInt = 10;
            }
            width += widthInt;
            cols++
          }
        })
        //超过10列，才出现滚动条，width>100才出现滚动条
        if(cols<=10){
            width = 100 ;
        }
        return `${width}%`;
    }

}

export default WeaTools.tryCatch(React,
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@jkkcqy`} msg={props.error ? props.error : getLabel( 383324,'对不起，该页面异常，请联系管理员' )} />,
    { error: "" }
)(JournalList);